package org.openkoala.koala.popup.generatecode;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.openkoala.koala.deploy.db2entity.DB2EntityUtil;
import org.openkoala.koala.exception.KoalaException;
import org.openkoala.koala.pojo.MavenProject;
import org.openkoala.koala.popup.BaseAction;
import org.openkoala.koala.util.ProjectParseUtil;
import org.openkoala.koala.util.ProjectQueryUtil;
import org.openkoala.koala.widget.Project;
/**
 * 菜单响应，及生成实体
 * @author lingen
 *
 */
public class GenerateEntityFromDatabaseAction extends BaseAction {

	/**
	 * 数据库操作的工具类
	 */
	private DB2EntityUtil db2EntityUtil;
	
	/**
	 * 领域层子项目
	 */
	private List<String> bizModules;
	
	@Override
	public void run(IAction action) {
		String projectPath = null;
		try {
			String selectedPath = getResource().getLocationURI().getPath();
			Project project = ProjectQueryUtil.getProject(selectedPath);
			projectPath = project.getPath() + File.separatorChar + project.getAppName();
			
			MavenProject parentProject = ProjectParseUtil.parseProject(projectPath);
			bizModules = parentProject.getBizModelProjects();
		} catch (KoalaException e) {
			popupErrorMessage(e.getMessage());
			e.printStackTrace();
		}
		db2EntityUtil = DB2EntityUtil.getDB2EntityUtil();
		DatabaseConfigDialog databaseConfigDialog = new DatabaseConfigDialog(
				shell, projectPath,db2EntityUtil);
		databaseConfigDialog.open();
		
		GenerateEntityDialog generateEntityDialog = new GenerateEntityDialog(shell,projectPath,this,this.db2EntityUtil,bizModules);
		generateEntityDialog.open();
	
	}
	
	void generateEntity(final DB2EntityUtil db2EntityUtil, final List<String> tables, final String projectPath, final String packageName) {
		final IResource resource = getResource();
		
		ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(shell);
		try {
			monitorDialog.run(true, true, new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor) throws InvocationTargetException,
						InterruptedException {
					monitor.beginTask("生成实体类...", 2);
					
					try {
						db2EntityUtil.connect();
						for (String tableName : tables) {
							db2EntityUtil.generateEntity(projectPath, tableName, packageName);
						}
						db2EntityUtil.closeConn();
					} catch (Exception e) {
						e.printStackTrace();
					}
					monitor.worked(1);
					
					monitor.setTaskName("刷新项目...");
					try {
						resource.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
					} catch (CoreException e) {
						e.printStackTrace();
					}
					monitor.worked(2);
					monitor.done();
				}
			});
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			try {
				//完成之后，关闭数据库连接
				db2EntityUtil.closeConn();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
